package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.QWorkDaily;
import com.neusoft.neuiotms.dcom.model.WorkDaily;
import com.neusoft.neuiotms.dcom.repository.CustomizedWorkDailyRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedWorkDailyRepositoryImpl extends AbstractRepository
        implements CustomizedWorkDailyRepository {

    @Override
    public Page<WorkDaily> search(final Pageable pageable, final Searchable searchable) {
        final QWorkDaily qWorkDaily = QWorkDaily.workDaily;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<WorkDaily> query = this.query().selectFrom(qWorkDaily);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    public long totalServiceTime(final Searchable searchable) {
        final QWorkDaily qWorkDaily = QWorkDaily.workDaily;
        final BooleanBuilder where = this.buildWhere(searchable);
        final Double total =
                this.query().select(qWorkDaily.totalWorkingHours.sum()).from(qWorkDaily).where(where).fetchOne();
        return null != total ? total.longValue() : 0;
    }

    @Override
    public List<WorkDaily> index(final Searchable searchable) {
        final QWorkDaily qWorkDaily = QWorkDaily.workDaily;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<WorkDaily> query = this.query().selectFrom(qWorkDaily);
        query.where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return WorkDaily.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QWorkDaily qWorkDaily = QWorkDaily.workDaily;

        if (!StringUtils.isEmpty(searchable.getStrValue("incidentUserName"))) {
            where.and(super.containsStr(qWorkDaily.incidentUserName, searchable, "incidentUserName"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("incidentUserCode"))) {
            where.and(super.equalsStr(qWorkDaily.incidentUserCode, searchable, "incidentUserCode"));
        }

        if (null != searchable.getLongValue("afEqReportDate")) {
            where.and(super.afterOrEqualsDate(qWorkDaily.dailyReportDate, searchable, "afEqReportDate"));
        }

        if (null != searchable.getLongValue("beEqReportDate")) {
            where.and(super.beforeOrEqualsDate(qWorkDaily.dailyReportDate, searchable, "beEqReportDate"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("unitName"))) {
            where.and(super.containsStr(qWorkDaily.unitName, searchable, "unitName"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("unitCode"))) {
            where.and(super.equalsStr(qWorkDaily.unitCode, searchable, "unitCode"));
        }

        if (null != searchable.getStrArray("institutionCodeArray")) {
            where.and(super.inStrings(qWorkDaily.unitCode, searchable, "institutionCodeArray"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("serviceTypeCode"))) {
            where.and(super.containsStr(qWorkDaily.serviceTypeCode, searchable, "serviceTypeCode"));
        }

        if (!StringUtils.isEmpty(searchable.getStrValue("serviceAttributesCode"))) {
            where.and(super.containsStr(qWorkDaily.serviceAttributesCode, searchable, "serviceAttributesCode"));
        }

        where.and(super.inStrings(qWorkDaily.serviceTypeCode, searchable, "serviceTypeCodeIn"));

        return where;
    }

}
